package com.jydd.singlekotlinmvvm.comm.feature.repo

import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.OnBackPressedCallback
import androidx.navigation.fragment.findNavController
import com.jydd.singlekotlinmvvm.R
import com.jydd.singlekotlinmvvm.comm.base.BaseFragment
import com.jydd.singlekotlinmvvm.comm.extensions.gone
import com.jydd.singlekotlinmvvm.comm.extensions.visible
import kotlinx.android.synthetic.main.fragment_repo_detail.*

/**
 * created by 韦敏敏
 * on 2020/1/21
 */
class RepoDetailFragment : BaseFragment() {
    override fun layout(): Int = R.layout.fragment_repo_detail
    private val callback = object : OnBackPressedCallback(true){
        override fun handleOnBackPressed() {
           if (webView.canGoBack()) webView.goBack()
            else findNavController().popBackStack()
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setHasOptionsMenu(true)
        val url = arguments?.getString("URL")
        webView.apply {
            settings.javaScriptEnabled = true
            webViewClient = object : WebViewClient(){}
            webChromeClient = object : WebChromeClient(){
                override fun onProgressChanged(view: WebView?, newProgress: Int) {
                    if (newProgress == 100){
                        loadingProgress.gone()
                    } else {
                        loadingProgress.visible()
                        loadingProgress.progress = newProgress
                    }
                    super.onProgressChanged(view, newProgress)
                }
            }
        loadUrl(url)
        }
        requireActivity().onBackPressedDispatcher.addCallback(callback)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        webView.destroy()
        callback.remove()
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, inflater)
        menu.clear()
    }
}